from django.shortcuts import render, HttpResponse, redirect
from django.conf import settings

from utils.tencent.sms import send_sms_single
# django中进行正则校验
from django.core.validators import RegexValidator
import random

from app01 import models
from django import forms


# Create your views here.
def send_sms(request):
    """
        发送短息
    :param request:
    :return:
    """
    tpl = request.GET.get('tpl')
    template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
    if not template_id:
        return HttpResponse('模版不存在')

    code = random.randrange(1000, 9999)
    response = send_sms_single('18622567995', template_id, [code, ])
    if response['result'] == 0:
        return HttpResponse('成功')
    return HttpResponse(response['errmsg'])


class RegisterModelForm(forms.ModelForm):
    """ 注册表单"""

    # 手机号
    mobile_phone = forms.CharField(
        label='手机号',
        validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$'), '手机号格式不正确', ]
    )

    password = forms.CharField(
        label='密码',
        # widget=forms.PasswordInput(attrs={'class': 'form-control'})
        widget=forms.PasswordInput()
    )

    confirm_password = forms.CharField(
        label='确认密码',
        widget=forms.PasswordInput()
    )

    code = forms.CharField(
        label='验证码',
        widget=forms.TextInput()
    )

    class Meta:
        model = models.UserInfo
        fields = ['username', 'email', 'password', 'confirm_password', 'mobile_phone', 'code']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入%s' % (field.label,)


def register(request):
    """
        用户信息注册
    :param request:
    :return:
    """
    #333
    form = RegisterModelForm()
    return render(request, 'app01/register.html', {'form': form})

def Con_list(request):
    "加速器列表"
    queryset=models.Accelerator.objects.all()
    return render(request,'app01/Con_list.html',{'queryset':queryset})
def Con_edit(request,nid):
    """加速器时长充值"""
    if request.method =="GET":
        obj=models.UserInfo.objects.filter(mobile_phone=nid)
        return render(request,'Con_edit.html',{'obj':obj})
    residue_timenum=request.POST.get("residue_timenum")
    models.UserInfo.objects.filter(mobile_phone=nid).update(residue_timenum="")
    return redirect("/Con/list")

def Con_delete(request):
    """删除加速器信息表"""
    nid=request.GET.get('nid')
    models.Department.objects.filter(de_mobile_phone=nid).delete()
    return redirect("/Con/list/")

def Us_list(request):
    """用户信息表"""
    queryset = models.UserInfo.objects.all()
    return render(request, 'app01/Us_list.html', {'queryset': queryset})

def Con_delete(request):
    """删除用户信息表"""
    nid=request.GET.get('nid')
    models.UserInfo.objects.filter(mobile_phone=nid).delete()
    return redirect("/Us/list/")
def Us_edit(request,nid):
    """用户名的编辑"""
    if request.method =="GET":
        obj=models.UserInfo.objects.filter(mobile_phone=nid)
        return render(request,'Us_edit.html',{'obj':obj})
    username=request.POST.get("username")
    models.UserInfo.objects.filter(mobile_phone=nid).update(username="")
    return redirect("/Us/list")
def Us_edit1(request,nid):
    """邮箱编辑"""
    if request.method == "GET":
        obj = models.UserInfo.objects.filter(mobile_phone=nid)
        return render(request, 'Us_edit1.html', {'obj': obj})
    email = request.POST.get("email")
    models.UserInfo.objects.filter(mobile_phone=nid).update(email="")
    return redirect("/Us/list")
def Us_edit2(request,nid):
    """密码编辑"""
    if request.method == "GET":
        obj = models.UserInfo.objects.filter(mobile_phone=nid)
        return render(request, 'Us_edit2.html', {'obj': obj})
    password = request.POST.get("password")
    models.UserInfo.objects.filter(mobile_phone=nid).update(password="")
    return redirect("/Us/list")
def Us_look(request):
    """用户信息查询"""
    if request.method == "GET":
        return render(request, 'Us_look.html')
    if request.method =="POST":
        nid = request.GET.get('nid')
        jdk= models.UserInfo.objects.filter(mobile_phone=nid)
        return redirect("/Us/lookout/",{"jdk":jdk})
def Con_look(request):
    """加速器信息查询"""
    if request.method == "GET":
        return render(request, 'Con_look.html')
    if request.method =="POST":
        nid = request.GET.get('nid')
        jdk= models.Accelerator.objects.filter(de_mobile_phone=nid)
        return redirect("/Con/lookout/",{"jdk":jdk})

